package com.jic.member.impl;

import com.jic.common.base.vo.AdminOperate;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.utils.IDUtils;
import com.jic.manager.api.rest.UserSysDeptApi;
import com.jic.member.constant.enums.DeleteFlagEnum;
import com.jic.manager.response.SysDeptResponse;
import com.jic.member.entity.MemDiscountThemeEntity;
import com.jic.member.mapper.MemDiscountThemeEntityMapper;
import com.jic.member.request.MemDiscountThemeRequest;
import com.jic.member.response.MemDiscountThemeResponse;
import com.jic.member.service.MemDiscountThemeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * description: MemDiscountThemeServiceImpl
 * date: 2020/7/1 10:21
 * author: wangys
 * version: 1.0
 */
@Service
public class MemDiscountThemeServiceImpl implements MemDiscountThemeService {

    @Autowired
    MemDiscountThemeEntityMapper memDiscountThemeEntityMapper;

    @Autowired
    private UserSysDeptApi userSysDeptApi;



    @Override
    public PageResult<MemDiscountThemeResponse> queryDiscountThemeList(MemDiscountThemeRequest request) {
        PageResult<MemDiscountThemeResponse> pageResult = new PageResult<>();

        request.setDeptId(request.getAdminOperate().getDeptId());
        Page page=new Page();
        page.setStart((request.getStart()-1)*request.getLimit());
        page.setLimit(request.getLimit());
        List<MemDiscountThemeResponse> list = memDiscountThemeEntityMapper.queryPage(request, page);
        List<Long> deptIds = list.stream().map(MemDiscountThemeResponse::getDeptId).collect(Collectors.toList());
        if(deptIds!= null&& deptIds.size()>0){
            List<SysDeptResponse> sysDeptResponseList = userSysDeptApi.queryListByIds(deptIds);
            for(MemDiscountThemeResponse entity : list){
                for(SysDeptResponse sysDeptResponse:sysDeptResponseList){
                    if(entity.getDeptId().equals(sysDeptResponse.getId())){
                        entity.setDeptName(sysDeptResponse.getDeptName());
                    }
                }
                entity.setEditFlag(request.getAdminOperate().getDeptId()==entity.getDeptId());
            }
        }
        int count = memDiscountThemeEntityMapper.queryPageCount(request);
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public MemDiscountThemeResponse queryDiscountThemeInfo(MemDiscountThemeRequest request) {
        MemDiscountThemeResponse response = new MemDiscountThemeResponse();
        MemDiscountThemeEntity memDiscountThemeEntity = memDiscountThemeEntityMapper.queryById(request.getId());
        BeanUtils.copyProperties(memDiscountThemeEntity, response);
        return response;
    }

    @Override
    public Long createDiscountTheme(MemDiscountThemeRequest request) {
        AdminOperate adminOperate = request.getAdminOperate();
        MemDiscountThemeEntity memDiscountThemeEntity = new MemDiscountThemeEntity();
        BeanUtils.copyProperties(request, memDiscountThemeEntity);
        memDiscountThemeEntity.setCreator(adminOperate.getOperator());
        memDiscountThemeEntity.setCreatorName(adminOperate.getOperateName());
        memDiscountThemeEntity.setUpdater(adminOperate.getOperator());
        memDiscountThemeEntity.setUpdaterName(adminOperate.getOperateName());
        memDiscountThemeEntity.setDeptType(adminOperate.getDeptType().getCode());
        memDiscountThemeEntity.setDeptId(adminOperate.getDeptId());
        if(memDiscountThemeEntity.getId() != null){
            memDiscountThemeEntityMapper.update(memDiscountThemeEntity);
        }else{
            memDiscountThemeEntity.setDiscountThemeCode(IDUtils.GenerateBillNo());
            memDiscountThemeEntityMapper.insert(memDiscountThemeEntity);
        }
        return memDiscountThemeEntity.getId();
    }

    @Override
    public Long deleteDiscountTheme(MemDiscountThemeRequest request) {
        MemDiscountThemeEntity memDiscountThemeEntity = new MemDiscountThemeEntity();
        memDiscountThemeEntity.setDeleteFlag(DeleteFlagEnum.DELETE.getCode());
        memDiscountThemeEntity.setId(request.getId());
        memDiscountThemeEntityMapper.update(memDiscountThemeEntity);
        return memDiscountThemeEntity.getId();
    }
}
